package Controller;

import Model.CheckRecord;
import Model.ExportFlyCheck;
import Model.MsgJson;
import Model.Staff;
import Service.CheckRecordService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;



import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 *
 * 跳转到飞行检查管理 直接返回json字符串并解析分页显示数据表格
 */

@Controller
@RequestMapping("/toTempCheck")
public class CheckRecordController {

    @Autowired
    CheckRecordService checkRecordService;

    //点击跳转护工信息详情界面
    @RequestMapping(value = "/detail")
    public String toHgDetail(HttpServletRequest request, HttpServletResponse response, HttpSession session){
        System.out.println("跳转查看护工信息");
        return "/SmartCheck/TemporaryCheck/HgDetail";

    }
    /**
     * @return跳转到新增检查页面
     */
    @RequestMapping(value="/toNewCheck")
    public String NewCheck(HttpServletRequest request,HttpServletResponse response,HttpSession session){
        System.out.println("跳转到新增飞行检查记录");
        return "/SmartCheck/TemporaryCheck/NewCheck";
    }

    /**
     * @return跳转到修改检查信息页面
     */
    @RequestMapping(value="/toChangeCheck")
    public String ChangeCheck(HttpServletRequest request,HttpServletResponse response,HttpSession session){
        System.out.println("跳转到编辑修改飞行检查记录");
        return "/SmartCheck/TemporaryCheck/ChangeCheck";
    }

    //等值联表查询护工信息
    @RequestMapping(value = "/detail/{hg_id}")
    @ResponseBody
    public Staff getHgDetail(@RequestParam(value = "pn",defaultValue = "12021")Integer pn,@RequestParam(value="limit",defaultValue="10")Integer limit,@PathVariable("hg_id")Integer hg_id) {
        //PageHelper.startPage(pn,limit);
        //列表显示条件查询记录
        //PageInfo page=new PageInfo(hgDetails);
        return checkRecordService.selectHgDetail(hg_id);
//        return MsgJson.success().add("pageInfo",page);
    }


    //导出记录（包括护工信息）
    @RequestMapping(value = "/export")
    @ResponseBody
    public PageInfo exportRecord() {
        //PageHelper.startPage(pn,limit);
        List<ExportFlyCheck> checkrecords = checkRecordService.exportCheckList();//列表显示所有记录
        PageInfo page=new PageInfo(checkrecords);
        return page;
//        return MsgJson.success().add("pageInfo",page);
    }

    //分页查询记录
    @RequestMapping(value = "/record")
    @ResponseBody
    public PageInfo getRecord(@RequestParam(value = "pn",defaultValue = "1")Integer pn,@RequestParam(value="limit",defaultValue="10")Integer limit) {
        PageHelper.startPage(pn,limit);
        List<CheckRecord> checkrecords = checkRecordService.getCheckList();//列表显示所有记录
        PageInfo page=new PageInfo(checkrecords);
        return page;
//        return MsgJson.success().add("pageInfo",page);
    }

    //条件查询记录
    @RequestMapping(value = "/selectrecord")
    @ResponseBody
    public PageInfo selectRecord(@RequestParam(value = "pn",defaultValue = "1")Integer pn,@RequestParam(value="limit",defaultValue="10")Integer limit,@Param("selectrecord")CheckRecord selectrecord) {
        PageHelper.startPage(pn,limit);
        List<CheckRecord> checkrecords = checkRecordService.selectCheckList(selectrecord);//列表显示条件查询记录
        PageInfo page=new PageInfo(checkrecords);
        return page;
//        return MsgJson.success().add("pageInfo",page);
    }


    //新增记录,参数 检查记录模型对象
    @RequestMapping(value = "/record", method = RequestMethod.POST)
    @ResponseBody
    public MsgJson newRecord(@Param("newRecord")CheckRecord newRecord){

        checkRecordService.addrecord(newRecord);

        return MsgJson.success();
    }

    @RequestMapping(value = "/upload")
    @ResponseBody
    public MsgJson uploadFile(@RequestParam(value = "uploadfile") MultipartFile file,HttpServletRequest request) throws IOException {

        //获取文件名
        String fileName=file.getOriginalFilename();
        //获取文件大小
        long size=file.getSize();
        //获取项目文件目录
        String realPath=request.getSession().getServletContext().getRealPath("/");

        //设置视频存放路径
        fileName= UUID.randomUUID().toString()+fileName.substring(fileName.lastIndexOf("."));//随机ID+后缀文件类型
        String path=realPath+"movies\\"+fileName;//拼接存储路径，webapp\movies\文件名.mp4等
        System.out.println(path);//控制台输出存储路径

        //文件上传到指定路径
        file.transferTo(new File(path));
        System.out.println("上传成功");
//        newRecord.setRecordfile(path);//设置上传文件地址
        path="movies/"+fileName;
        return MsgJson.success().add("path",path);
    }

    //修改记录,参数 id 检查记录模型对象
    @RequestMapping(value = "/record/{recordId}", method = RequestMethod.POST)
    @ResponseBody
    public MsgJson updateRecord(@PathVariable("recordId") Integer recordId, @Param("newrecord")CheckRecord newrecord) {

        checkRecordService.updaterecord(recordId, newrecord);

        return MsgJson.success();


    }

    //删除记录,参数 id 检查记录模型对象
    @RequestMapping(value = "/record/{recordId}", method = RequestMethod.DELETE)
    @ResponseBody
    public MsgJson deleteRecord(@PathVariable("recordId") Integer recordId) {

        checkRecordService.deleterecord(recordId);

        return MsgJson.success();

    }

}